﻿using DevExpress.Data.Filtering;
using Volo.Abp.DependencyInjection;
using YAPACS.BusinessObjects.Exams;
using YAPACS.Workflows;

namespace YAPACS.RisServer.Workflows.TabPanel.Queue;

public class Expression:WorkflowBase, IExpression,ITransientDependency
{
    protected override async Task<WorkflowOutput> InvokeCoreAsync(WorkflowInput input)
    {
        const string key = "Input";
        CriteriaOperator criteriaOperator = CriteriaOperator.FromLambda<BusinessObjects.Exams.Exam>(e => true);
        if (input.ContainsKey(key) && input.GetValueOrDefault(key) is Dictionary<string, object> search)
        {
            //返回CriteriaOperator
            //队列状态
            var statusString = search.GetValueOrDefault("status") as string;
            if (!string.IsNullOrWhiteSpace(statusString))
            {
                var status = statusString switch
                {
                    "wait" => ExamQueuingStatus.Wait,
                    "ready" => ExamQueuingStatus.Ready,
                    "skiped" => ExamQueuingStatus.Skip,
                    "pending" => ExamQueuingStatus.Pending,
                    "completed" => ExamQueuingStatus.Completed,
                    _ => ExamQueuingStatus.Wait
                };
                var criteria = CriteriaOperator.FromLambda<BusinessObjects.Exams.Exam>(e => e.ExamQueuing.Status == status);
                criteriaOperator = CriteriaOperator.And(criteriaOperator, criteria);
            }
        }
        input.Add("Output", criteriaOperator);
        return await Task.FromResult(new WorkflowOutput
        {
            Success = true
        });
    }
}
